package cn.flightcloud.service.file.service;

import java.io.File;
import java.util.concurrent.locks.ReentrantLock;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import cn.flightcloud.service.file.MutilFileInfo;
import cn.flightcloud.service.file.web.MutilFileUploadUtils;

@Service
public class FileManagerServiceImpl implements FileManagerService {

	@Value("d:/test/temp/")
	private String tempWorkPath;// 分片临时文件存放目录
	@Value("d:/test/")
	private String saveFilePath;// 文件存放目录
	private ReentrantLock filetempLock = new ReentrantLock();

	@Override
	public void saveMutiBurstFiletoDir(MutilFileInfo fileinfo, MultipartFile file) throws Exception {
		this.checkBaseDir(tempWorkPath);
		File tempFile = this.GenerateDirPathForCurrFile(fileinfo, "chunks");
		MutilFileUploadUtils.spaceFileWriter(file, tempFile, fileinfo);
	}

	@Override
	public void saveSingleFiletoDir(MutilFileInfo fileinfo, MultipartFile file) throws Exception {
		this.checkBaseDir(saveFilePath);
		File targetFile = this.GenerateDirPathForCurrFile(fileinfo, "single");
		MutilFileUploadUtils.saveFile2DirPath(file, targetFile);
	}

	@Override
	synchronized public File GenerateDirPathForCurrFile(MutilFileInfo fileinfo, String flag) throws Exception {
		String fileName = fileinfo.getName();
		String lastModifiedDate = fileinfo.getLastModifiedDate();
		long fileSize = fileinfo.getSize();
		String type = fileinfo.getType();
		String id = fileinfo.getId();
		String extName = fileName.substring(fileName.lastIndexOf("."));
		long timeStemp = System.currentTimeMillis();
		if ("single".equals(flag)) {
			String fileNameSource = fileName + lastModifiedDate + fileSize + type + id + timeStemp;
			String fileDirName = SHA256Util.getSHA256StrJava(fileNameSource) + extName;
			File targetFile = new File(saveFilePath, fileDirName);
			while (targetFile.exists()) {
				fileNameSource = fileNameSource + "1";
				fileDirName = SHA256Util.getSHA256StrJava(fileNameSource) + extName;
				targetFile = new File(fileDirName);
			}
			return targetFile;
		} else if ("chunks".equals(flag)) {
			String fileNameSource = fileSize + "_" + fileName + id + lastModifiedDate;
			String fileDirName = tempWorkPath + "/" + SHA256Util.getSHA256StrJava(fileNameSource) + extName + ".temp";
			File tempFile = new File(fileDirName);
			if (!(tempFile.exists() && tempFile.isFile())) {
				filetempLock.lock();// 上锁
				if (!(tempFile.exists() && tempFile.isFile())) {
					MutilFileUploadUtils.readySpaceFile(fileinfo, tempFile);
				}
				filetempLock.unlock();// 释放锁
			}
			tempFile = new File(fileDirName);
			return tempFile;
		} else {
			throw new Exception("目标文件生成失败");
		}
	}

	public void checkBaseDir(String baseDir) throws Exception {
		File file = new File(baseDir);
		if (!file.exists() && !file.isDirectory()) {
			file.mkdirs();
		}
	}

	@Override // 文件合并
	public void MutilMergingChunks(MutilFileInfo fileinfo) throws Exception {
		// TODO Auto-generated method stub
		String fileName = fileinfo.getName();
		String lastModifiedDate = fileinfo.getLastModifiedDate();
		long fileSize = fileinfo.getSize();
		String id = fileinfo.getId();
		String extName = fileName.substring(fileName.lastIndexOf("."));
		String fileNameSource = fileSize + "_" + fileName + id + lastModifiedDate;
		String fileDirName = tempWorkPath + "/" + SHA256Util.getSHA256StrJava(fileNameSource) + extName + ".temp";
		File tempFile = new File(fileDirName);
		if (tempFile.exists() && tempFile.isFile()) {
			checkBaseDir(saveFilePath);
			String targetDirName = saveFilePath + "/" + SHA256Util.getSHA256StrJava(fileNameSource);
			File targetFile = new File(targetDirName + extName);
			while (targetFile.exists() && targetFile.isFile()) {
				targetDirName = targetDirName + "1";
				targetFile = new File(targetDirName + extName);
			}
			System.out.println(targetFile.getAbsolutePath());
			if (tempFile.renameTo(targetFile)) {
				System.out.println("文件重命名成功!");
				// 数据库操作
				// 数据库操作
			} else {
				System.out.println("文件重命名失败!");
				throw new Exception("临时文件重命名失败");
			}

		}
	}
}
